package com.zy.composeworld.ui.screens.image.canvas

import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.OvalShape
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PointMode
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.drawscope.scale
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.graphics.drawscope.withTransform
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.drawText
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.toSize
import coil.compose.AsyncImage
import com.zy.composeworld.R
import kotlin.math.roundToInt


@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun CanvasPager001() {

    LazyColumn(
        modifier = Modifier
            .padding(16.dp)
            .background(color = Color.Gray),
        verticalArrangement = Arrangement.spacedBy(16.dp),
    ) {


        item {
            TextHead("Compose 中的图形")

            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                val canvasQuadrantSize = size / 2F
                drawRect(
                    color = Color.Magenta, size = canvasQuadrantSize
                )
            }




            TextHead("缩放")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                drawCircle(Color.Red, radius = 20.dp.toPx())
                scale(scaleX = 2f, scaleY = 0.5f) {
                    drawCircle(Color.Blue, radius = 20.dp.toPx())
                }
            }
            TextHead("平移")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {

                translate(left = 50f) {
                    drawCircle(Color.Green, radius = 20.dp.toPx())
                }

            }


            TextHead("旋转")
            Row {


                Canvas(
                    modifier = Modifier
                        .size(100.dp)
                        .border(1.dp, color = Color.Blue)
                ) {
                    rotate(degrees = 45F) {
                        drawRect(
                            color = Color.Green,
                            topLeft = Offset(x = size.width / 3F, y = size.height / 3F),
                            size = size / 3F
                        )
                    }
                }

                Canvas(
                    modifier = Modifier
                        .size(100.dp)
                        .border(1.dp, color = Color.Blue)
                ) {
                    rotate(degrees = 30F) {
                        drawRect(
                            color = Color.Green,
                            topLeft = Offset(x = size.width / 3F, y = size.height / 3F),
                            size = size / 3F
                        )
                    }
                }

                Canvas(
                    modifier = Modifier
                        .size(100.dp)
                        .border(1.dp, color = Color.Blue)
                ) {
                    rotate(degrees = 60F) {
                        drawRect(
                            color = Color.Green,
                            topLeft = Offset(x = size.width / 3F, y = size.height / 3F),
                            size = size / 3F
                        )
                    }
                }
            }


            TextHead("边衬区")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                val canvasQuadrantSize = size / 2F
                inset(horizontal = 50f, vertical = 30f) {
                    drawRect(color = Color.Green, size = canvasQuadrantSize)
                }
            }

            TextHead("多个转换")

            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                withTransform({
                    translate(left = size.width / 5F)
                    rotate(degrees = 45F)
                }) {
                    drawRect(
                        color = Color.Green,
                        topLeft = Offset(x = size.width / 3F, y = size.height / 3F),
                        size = size / 3F
                    )
                }
            }

            TextHead("绘制文本")
            val textMeasurer = rememberTextMeasurer()

            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                drawText(textMeasurer, "Hello")
            }


            TextHead("绘制图片")
            val dogImage = ImageBitmap.imageResource(id = R.drawable.ic_dog2)

            Canvas(modifier = Modifier
                .size(100.dp)
                .border(1.dp, color = Color.Blue), onDraw = {
                drawImage(dogImage)
            })

        }

        item {
            TextHead("绘制基本形状")
            TextHead("drawCircle")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
                    .padding(16.dp),
                onDraw = {
                    drawCircle(Color.Green)
                }
            )
        }

        item {
            TextHead("drawRect")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
                    .padding(16.dp),
                onDraw = {
                    drawRect(Color.Green)
                }
            )
        }

        item {
            TextHead("drawRoundRect")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
                    .padding(16.dp),
                onDraw = {
                    drawRoundRect(Color.Green, cornerRadius = CornerRadius(8F, 8F))
                }
            )
        }

        item {
            TextHead("Line")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                val canvasWidth = size.width
                val canvasHeight = size.height
                drawLine(
                    start = Offset(x = canvasWidth, y = 0f),
                    end = Offset(x = 0f, y = canvasHeight),
                    color = Color.Blue
                )

            }
        }


        item {
            TextHead("drawOval")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                drawOval(
                    color = Color.Blue
                )

            }
        }


        item {
            TextHead("drawPoints")
            Canvas(
                modifier = Modifier
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            ) {
                drawPoints(
                    points = arrayListOf(Offset(0f, 0f), Offset(50f, 50f)),
                    pointMode = PointMode.Points,
                    color = Color.Blue,
                    strokeWidth = 4F
                )

            }
        }

        item {
            TextHead("drawPath")
            Spacer(
                modifier = Modifier
                    .drawWithCache {
                        val path = Path()
                        path.moveTo(0f, 0f)
                        path.lineTo(size.width / 2f, size.height / 2f)
                        path.lineTo(size.width, 0f)
                        path.close()
                        onDrawBehind {
                            drawPath(path, Color.Magenta, style = Stroke(width = 10f))
                        }
                    }
                    .size(100.dp)
                    .border(1.dp, color = Color.Blue)
            )
        }

        item {
            TextHead("访问 Canvas 对象")
            val drawable = ShapeDrawable(OvalShape())
            Spacer(
                modifier = Modifier
                    .drawWithContent {
                        drawIntoCanvas { canvas ->
                            drawable.setBounds(0, 0, size.width.toInt(), size.height.toInt())
                            drawable.draw(canvas.nativeCanvas)
                        }
                    }
                    .size(200.dp, 100.dp)
                    .border(1.dp, color = Color.Blue)
            )
        }


    }

}

@Composable
private fun TextHead(text: String) {
    Text(text, fontWeight = FontWeight.Bold, color = Color.Red)
}